package com.example.food03;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.food03.strongweb_bool.HTMLtool;
import com.example.food03.web_bool.Order;
import com.example.food03.web_bool.User;
import com.google.gson.Gson;


public class LoginActivity extends AppCompatActivity {
    public Button b1;
    public EditText name,pwd;
    public static String html,users,users2,h_order,orders = "";//接收信息
    public static int pwd2;
    public static String address=new String();
    public static int q,w=0;//一些逻辑判断
    //评论具体的信息
    public static String[][] order_foodname=new String[10][20];
   public static String[][] order_shopname=new String[10][20];
   public static String[][] order_comment=new String[10][20];
   public static String[][] order_time=new String[10][20];
    public static double[][] order_foodprice=new double[10][20];
    public static int[][] order_foodnum=new int[10][20];
    public static int[] tiaoshu_sum=new int[10];
    String[] o,or={};
    User user;//使用User类
    Order order;//使用Order类
    Gson gson=new Gson();//json类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        b1=findViewById(R.id.button);
        name=findViewById(R.id.editTextTextPersonName);
        pwd=findViewById(R.id.editTextTextPassword);
       new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获取所有的店铺信息
                    html = HTMLtool.getHtml("http://192.168.43.126:8080/foodService22/getAllUsers.do");
                    users=html.substring(1, html.length()-2);
                     o = users.split("\\},");
                    //获取所有的评论信息
                    h_order=HTMLtool.getHtml("http://192.168.43.126:8080/foodService22/getAllOrder.do");
                    orders=h_order.substring(1,h_order.length()-2);
                    or=orders.split("\\},");
                    for(int i = 0;i<o.length;i++){
                        //System.out.print(" " + o[i]);
                        o[i]=o[i]+"}";
                        //Log.d("用户2",o[i]);
                    }
                    for(int i=0;i<or.length;i++){
                        or[i]=or[i]+"}";
                    }
                    // Log.i("json2", or.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<o.length;i++){
                user=gson.fromJson(o[i],User.class);
               //Log.d("jjjj",user.getUsername());
                    if((user.getUsername().equals(name.getText().toString().trim()))&&(user.getUserpass().equals(pwd.getText().toString().trim()))){
                            users2=user.getUsername();
                            pwd2=user.getUser_id();
                            address=user.getAddress();
                        int u=0;
                        //Log.d("--11",pwd2+"");
                        for(int j=0;j<or.length;j++){
                           // Log.d("--",or[j]+"");
                            order=gson.fromJson(or[j],Order.class);
                            if(order.getUser_id()==pwd2){
                                order_foodname[pwd2][u]=order.getFoodname();
                                order_shopname[pwd2][u]=order.getShopname();
                                order_foodnum[pwd2][u]=order.getNum();
                                order_foodprice[pwd2][u]=order.getPrice();
                                order_comment[pwd2][u]=order.getContent();
                                order_time[pwd2][u]=order.getOrdertime();
                                u++;
                               // Log.d("--",u+"");
                            }
                        }
                        tiaoshu_sum[pwd2]=u;
                        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                            MainActivity.q=1;
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            q=1;
                            return;
                    }
                }

                 if (q==0){
                    Toast.makeText(LoginActivity.this,"密码或用户名错误！",Toast.LENGTH_LONG).show();
                  /*  name.setText("");
                    pwd.setText("");*/
                }
                /*Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);*/
            }
        });
    }
}